package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.RoleMapper;
import com.example.mapper.RolePrivilegeMapper;
import com.example.mapper.UserRoleMapper;
import com.example.pojo.PageBean;
import com.example.pojo.Role;
import com.example.pojo.RolePrivilege;
import com.example.pojo.UserRole;
import com.example.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-20
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RolePrivilegeMapper rolePrivilegeMapper;


    @Override
    public boolean removeRole(Integer id) {
        //删除岗位与用户关系信息
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null,UserRole::getRid,id);
        int delete = userRoleMapper.delete(wrapper);
        //删除岗位与权限关系
        LambdaQueryWrapper<RolePrivilege> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(id != null, RolePrivilege::getRid, id);
        int delete1 = rolePrivilegeMapper.delete(wrapper1);

        //删除岗位信息
        boolean remove = this.removeById(id);

        return remove && delete > 0 && delete1 > 0;
    }

    @Override
    public PageBean<Role> getPage(Integer currentPage, Integer pageSize) {
        Integer page = (currentPage - 1) * pageSize;
        List<Role> list = userRoleMapper.getPageList(page,pageSize);

        return new PageBean<>(currentPage,pageSize,count(),list);
    }
}
